package com.jzr.open.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.common.util.U;
import com.jzr.open.common.SecurityHelper;
import com.jzr.open.db1.mapper.THospitalMapper;
import com.jzr.open.db1.model.THospital;
import com.jzr.open.db1.service.HospitalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HospitalServiceImpl extends ServiceImpl<THospitalMapper,THospital> implements HospitalService {
    public List<Integer> getIdList(Wrapper wrapper) {
        return baseMapper.getIdList(wrapper);
    }

    public Integer getId(Wrapper wrapper) {
        List<Integer> list = getIdList(wrapper);
        return list == null || list.size() <= 0 ? null : list.get(0);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(THospital record) {
        if(U.isBlank(record) || U.isBlank(record.getName())){
            return false;
        }
        Integer userId = SecurityHelper.getUserId();
        record.setUpdateBy(userId);
        record.setUpdateTime(LocalDateTime.now());
        int count;
        Integer id = getId(null);
        if(id == null){
            count = baseMapper.insert(record);
        }else{
            record.setId(id);
            count = baseMapper.updateById(record);
        }
        return count>0;
    }

}
